package com.bree.erp.service.stateless;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.bree.erp.entity.PrefixSequence;
import com.bree.erp.service.PrefixSequenceService;

@Stateless
public class PrefixSequenceServiceBean implements PrefixSequenceService {

	@PersistenceContext()
	private EntityManager em;
	
	@Override
	public PrefixSequence create(String prefix) {
		
		PrefixSequence entity = new PrefixSequence();
		entity.setPrefix(prefix);
		
		em.persist(entity);
		
		return entity;
	}

	@Override
	public PrefixSequence read(Long id) {
		
		return em.find(PrefixSequence.class, id);
	}

	@Override
	public PrefixSequence update(Long id) {
		PrefixSequence entity = read(id);
		
		Query query = em.createNamedQuery(PrefixSequence.HQL_COUNT_PREFIX)
				.setParameter("id", id)
				.setParameter("prefix", entity.getPrefix());
		
		Long sequence = (Long) query.getSingleResult();
		
		
		entity.setSequence(sequence);
		
		return entity;
	}

}
